package com.fsj.miniapp.query;

import com.fsj.miniapp.states.DeleteState;
import com.stip.mybatis.generator.plugin.BaseExample;
import org.springframework.util.CollectionUtils;

import java.util.List;

public class QueryBeanParsing {

    public static BaseExample convertQueryBeanToBaseExample(QueryBean queryBean) {

        BaseExample example = new BaseExample();

        List<QueryParams> queryParamsList = queryBean.getQueryParamsList();
        BaseExample.Criteria criteria = example.createCriteria();
        criteria.addCriterion("mark_for_delete", DeleteState.TRUE.getState());
        if (!CollectionUtils.isEmpty(queryParamsList)) {

            for (QueryParams queryParams : queryParamsList) {
                String property = queryParams.getProperty() + " " +  ConditionState.getValueByKey(queryParams.getOperator());

                String value = queryParams.getValue();
                if (queryParams.getOperator().equalsIgnoreCase(ConditionState.CN.getValue())) {
                    value = "%" + value + "%";
                }

                criteria.addCriterion(property, value);
            }
        }
        example.setOrderByClause(queryBean.getSorted());

        return example;
    }
}
